﻿using OdooRpcClient.Model.Sale;
using PortaCapena.OdooJsonRpcClient;
using PortaCapena.OdooJsonRpcClient.Consts;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using WebAssistant.BusinessLogic;
using WebAssistant.BusinessLogic.Model;
using WebAssistant.Common;
using static Assistant.Controllers.PrintModel;

namespace Assistant.Report.Method.Sale
{
    /// <summary>
    /// 打印 报价单/订单 方法
    /// </summary>
    public class ReportSaleOrder : ReportAction
    {
        public static async Task<DataTable> GetOrderDataTable()
        {
            DataTable dt = new DataTable("Orders");

            var orderRepository = new OdooRepository<SaleOrderOdooModel>(Shared.odooConfig);
            orderRepository.Config.Context.Language = context.lang;

            var orders = await orderRepository.Query()
                .Where(x => x.Id, OdooOperator.In, context.active_ids)
                .Select(x => new
                {
                    x.Name,
                    x.OrderLine,//订单行
                    x.PartnerId, //客户
                    x.DateOrder, //单据日期
                    x.Reference, //付款参考
                    x.ClientOrderRef, //客户订单号
                    x.UserId, //销售员
                    //x.OrderLine, //订单行
                })
                .ToListAsync();

           
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Customer", typeof(string));
            dt.Columns.Add("Address", typeof(string));
            dt.Columns.Add("ClientOrderRef", typeof(string));
            dt.Columns.Add("DateOrder", typeof(DateTime));
            dt.Columns.Add("Seller", typeof(string));

            foreach(var order in orders.Value)
            {
                DataRow dr = dt.NewRow();
                dr["Name"] = order.Name;
                dr["Customer"] = DataAccess.GetPartnerByPartnerId(Convert.ToInt32(order.PartnerId)).Name;
                dr["Address"] = StringHelper.ReplaceStringWrap(DataAccess.GetPartnerByPartnerId(Convert.ToInt32(order.PartnerId)).ContactAddress," ");
                dr["ClientOrderRef"] = order.ClientOrderRef;
                dr["DateOrder"] = order.DateOrder;
                dr["Seller"] = DataAccess.GetUserByUserId(Convert.ToInt32(order.UserId)).Name ;
                dt.Rows.Add(dr);
            }
            return dt;
        }

        public static async Task<DataTable> GetOrderLineDataTable()
        {
            DataTable dt = new DataTable("Lines");
            //获取订单行
            var orderLinesRepository = new OdooRepository<SaleOrderLineOdooModel>(Shared.odooConfig);
            orderLinesRepository.Config.Context.Language = context.lang;
            var orderLines = await orderLinesRepository.Query()
                 .Where(x => x.OrderId, OdooOperator.In, context.active_ids)
            .Select(x => new
            {
                x.ProductId,
                x.Name,//说明
                x.DisplayName,//显示名称
                x.PriceUnit, //单价
                x.TaxId,
                x.ProductUomQty, //数量
                x.PriceSubtotal, //小计
                x.PriceTotal, //合计

                })
            .ToListAsync();

            dt.Columns.Add("ProductName", typeof(string));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("ProductUomQty", typeof(double));
            dt.Columns.Add("PriceUnit", typeof(double));
            dt.Columns.Add("Tax", typeof(string));
            dt.Columns.Add("PriceSubtotal", typeof(decimal));
            foreach (var line in orderLines.Value)
            {
                //DataRow dr = dt.NewRow();
                //int ProductId = DataAccess.GetProductByProductId(Convert.ToInt32(line.ProductId)).ProductTmplId;
                //dr["ProductName"] = DataAccess.GetProductTemplatesByProductId(ProductId).Name;

                //dr["Name"] = line.Name;
                //dr["ProductUomQty"] = line.ProductUomQty;
                //dr["PriceUnit"] = line.PriceUnit;
                //dr["Tax"] = DataAccess.GetTaxByTaxId(Convert.ToInt32(line.TaxId)).Name;
                //dr["PriceSubtotal"] = line.PriceSubtotal;
                //dt.Rows.Add(dr);
            }
            return dt;
        }

        public static DataTable GetReportParametersDataTable()
        {
            DataTable dt = new DataTable("Parameters");
            dt.Columns.Add("Title", typeof(string));
            dt.Columns.Add("CompanyName", typeof(string));
            dt.Columns.Add("CompanyLogo", typeof(string));
            dt.Columns.Add("Country", typeof(string));
            dt.Columns.Add("Zip", typeof(string));
            dt.Columns.Add("City", typeof(string));
            dt.Columns.Add("State", typeof(string));
            dt.Columns.Add("Street", typeof(string));
            dt.Columns.Add("Street2", typeof(string));
            dt.Columns.Add("StreetNumber", typeof(string));
            dt.Columns.Add("Telephone", typeof(string));
            dt.Columns.Add("Email", typeof(string));
            dt.Columns.Add("Website", typeof(string));

            DataRow dr = dt.NewRow();
            dr["Title"] = action.Name;

            PrintContext context = action.Context;
            int companyId = context.allowed_company_ids[0];
            Companies company = DataAccess.GetCompanyByCompanyId(companyId);
            dr["CompanyName"] = company.Name;
            dr["CompanyLogo"] = company.Logo;
            dr["Country"] = DataAccess.GetCountryByCountryId(company.CountryId).Name;
            dr["Zip"] = company.Zip;
            dr["City"] = company.City;
            dr["State"] = DataAccess.GetStateByStateId(company.StateId).Name;
            dr["Street"] = company.Street;
            dr["Street2"] = company.Street2;
            dr["StreetNumber"] = company.StreetNumber;
            dr["Telephone"] = company.Phone;
            dr["Email"] = company.Email;
            dr["Website"] = company.Website;
            dt.Rows.Add(dr);
            return dt;
        }
    }
}
